# infrastructure for makefilet itself

# run jobs in parallel (if more than one CPU core is available)
# Call make with the argument "-j 1" in order to disable automatic parallel processing.
ifneq "$(findstring -j,$(MAKEFLAGS)))" ""
	MAX_PARALLEL_JOBS ?= $(shell { nproc || getconf "_NPROCESSORS_ONLN" || echo "1"; } 2>/dev/null)
	ifneq "$(MAX_PARALLEL_JOBS)" ""
		MAKEFLAGS += --jobs=$(MAX_PARALLEL_JOBS)
		# group output by target (no interleaving of output from different targets)
		ifneq "$(findstring -O,$(MAKEFLAGS)))" ""
			MAKEFILET_OUTPUT_SYNCHRONIZATION ?= line
			MAKEFLAGS += --output-sync=$(MAKEFILET_OUTPUT_SYNCHRONIZATION)
		endif
	endif
endif


ifneq "$(MAKEFILET_DOWNLOAD_VERSION)" ""
	# the requested download version shall not be newer than the active version
	ifneq "$(MAKEFILET_DOWNLOAD_VERSION)" "$(shell printf '%s\n' '$(MAKEFILET_DOWNLOAD_VERSION)' 'v$(MAKEFILET_CURRENT_VERSION)' | sort --version-sort | head -1)"
		# "$(warning ...)" is not allowed outside of a target - thus we inject an error message via an assignment
		_ = $(shell echo >&2 "WARNING: The currently active version of makefilet is older than the version specified via 'MAKEFILET_DOWNLOAD_VERSION' (v$(MAKEFILET_CURRENT_VERSION) < $(MAKEFILET_DOWNLOAD_VERSION)).")
	endif
endif
